package com.empresa.iflex.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class IFlexDbAdapter{
	
	private static final String DATABASE_NAME = "iflex.db";
	private static final int DATABASE_VERSION = 4;
 
    public static final String PERCURSO_TABLE_NAME = "percurso";
    public static final String CARRO_TABLE_NAME = "carro";
    
    private static final String PERCURSO_TABLE_CREATE =
                "CREATE TABLE " + PERCURSO_TABLE_NAME + " (" +
                "_id" + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "distancia" + " REAL, " +
                "percurso" + " TEXT, " +
                "consumo" + " REAL, " +
                "media" + " REAL, " +
                "combustivel" + " TEXT, " +
                "preco" + " REAL, " +
                "carro_id" + " INTEGER, " +
                "tanque_cheio" + " BOOLEAN, " +
                "data" + " DATETIME DEFAULT CURRENT_TIMESTAMP, " +
                "FOREIGN KEY(carro_id) REFERENCES carro(_id));";
    
    private static final String CARRO_TABLE_CREATE = "CREATE TABLE "+ CARRO_TABLE_NAME + " (" +
     			" _id " + "INTEGER PRIMARY KEY AUTOINCREMENT, " + 
     			" carro " + " TEXT NOT NULL, " +
     			" tanque " + " REAL NOT NULL);";

    private final Context mCtx;
    private IFlexDbHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static class IFlexDbHelper extends SQLiteOpenHelper {

        IFlexDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        	db.execSQL(CARRO_TABLE_CREATE);
        	db.execSQL(PERCURSO_TABLE_CREATE);          
            
            ContentValues values = new ContentValues();
            values.put("carro", "Carro");
            values.put("tanque", "0");
            db.insert(CARRO_TABLE_NAME, null, values);
        }

    	@Override
    	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    		Log.w("iFlex - ", "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS percurso");
            db.execSQL("DROP TABLE IF EXISTS carro");
            onCreate(db);
    	}
    }
    
    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public IFlexDbAdapter(Context ctx){
    	mCtx = ctx;
    }

    /**
     * Open the notes database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public SQLiteDatabase open() throws SQLException {
        mDbHelper = new IFlexDbHelper(mCtx);
        mDb = mDbHelper.getReadableDatabase();
        return mDb;
    }
    
    public SQLiteDatabase wOpen() throws SQLException {
        mDbHelper = new IFlexDbHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return mDb;
    }
    
    public void close() {
        mDbHelper.close();
    }

}
